package com.bm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bm.Result.CodeEnum;
import com.bm.Vo.Notice.DetailNoticeVo;
import com.bm.Vo.Notice.UpdateNoticeVo;
import com.bm.constant.Constants;
import com.bm.exception.BmException;
import com.bm.mapper.*;
import com.bm.model.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bm.page.PageBean;
import com.bm.query.Common.InfoQuery;
import com.bm.query.Notice.NoticeListQuery;
import com.bm.query.Notice.NoticeNewUserNotificationQuery;
import com.bm.service.EjyyNoticeToUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
*
*/
@Service
public class EjyyNoticeToUserServiceImpl extends ServiceImpl<EjyyNoticeToUserMapper, EjyyNoticeToUser>
implements EjyyNoticeToUserService{

   @Autowired
   private EjyyNoticeToUserMapper ejyyNoticeToUserMapper;

   @Autowired
   private EjyyNoticeTplMapper ejyyNoticeTplMapper;

   @Autowired
   private EjyyPropertyCompanyUserMapper propertyCompanyUserMapper;

   @Autowired
   private EjyyWechatMpUserMapper ejyyWechatMpUserMapper;

   @Autowired
   private EjyyBuildingInfoMapper ejyyBuildingInfoMapper;



    /**
     * 分页条件查询
     * @return
     */
    @Override
    public PageBean Pagination(NoticeListQuery noticeListQuery) {
        //分页条件构造器
//        LambdaQueryWrapper<EjyyNoticeToUser> queryWrapper = new LambdaQueryWrapper<>();
//        //添加条件查询
//        queryWrapper.eq(EjyyNoticeToUser::getPublished,published);
//        queryWrapper.like(communityId!=null,EjyyNoticeToUser::getCommunityId,communityId);
//        queryWrapper.orderByDesc(EjyyNoticeToUser::getPublishedAt);
//        return queryWrapper;
        Integer pageNum = noticeListQuery.getPageNum();
        Integer pageSize = noticeListQuery.getPageSize();
        Long communityId = noticeListQuery.getCommunityId();
        Boolean published = noticeListQuery.getPublished();

        PageBean pageBean = new PageBean();

        int page=(pageNum-1)*pageSize;
        //当前页索引
        pageBean.setPageIndex(pageNum);
        //页大小
        pageBean.setPageSize(pageSize);

        EjyyNoticeToUser ejyyNoticeToUser = new EjyyNoticeToUser();
        ejyyNoticeToUser.setCommunityId(communityId);
        ejyyNoticeToUser.setPublished(published);
        //总条数
        pageBean.setTotalRecord(ejyyNoticeToUserMapper.selectAllNoticeToUserNumber(ejyyNoticeToUser));
        //添加条件和数据到分页构造器
        pageBean.setBeanList(ejyyNoticeToUserMapper.selectAllNoticeToUser(page,pageSize,published,communityId));
        //计算开始页和结束页
        pageBean.setPageBeginAndPageEnd();
        return pageBean;
    }

    /**
     * 新增用户通知数据
     * @param noticeNewUserNotificationQuery
     * @return
     */
    @Transactional
    @Override
    public void VerifyData(NoticeNewUserNotificationQuery noticeNewUserNotificationQuery) {

        EjyyNoticeToUser ejyyNoticeToUser = new EjyyNoticeToUser();
        EjyyNoticeTpl ejyyNoticeTpl = new EjyyNoticeTpl();

        //判断是否添加通知模板数据
        int insert =0;
        if (noticeNewUserNotificationQuery.getOaTplMsg()){
            //拷贝对象
            BeanUtil.copyProperties(noticeNewUserNotificationQuery,ejyyNoticeTpl);
            //新增操作
            insert=ejyyNoticeTplMapper.insert(ejyyNoticeTpl);
        }
        ejyyNoticeToUser.setRefer(true);
        //获取当前创建时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = dateFormat.format(date);
        ejyyNoticeToUser.setCreatedAt(Long.valueOf(format));

        //判断通知是否发布
        if (noticeNewUserNotificationQuery.getPublished()){
            ejyyNoticeToUser.setPublishedAt(Long.valueOf(format));
            ejyyNoticeToUser.setPublishedBy(Constants.ADMIN_ID);
        }
        //拷贝对象
        BeanUtil.copyProperties(noticeNewUserNotificationQuery,ejyyNoticeToUser);

        //判断通知模板是否新增数据
        if (insert>0){
            //获取NoticeTplId
            Long noticeTplId = ejyyNoticeTpl.getId();
            ejyyNoticeToUser.setNoticeTplId(noticeTplId);
        }
        //新增通知
        ejyyNoticeToUserMapper.insert(ejyyNoticeToUser);
        //判断通知是否发布
        if (noticeNewUserNotificationQuery.getPublished()){
            /**
             * 查询小区通知广播
             */
            //            LambdaQueryWrapper<EjyyNoticeToUser> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(EjyyNoticeToUser::getNoticeTplId,noticeTplId);
//            //查询刚添加到数据库数据
//            EjyyNoticeToUser ejyyNoticeToUser1 = ejyyNoticeToUserMapper.selectOne(wrapper);
//            Long ToUserId = ejyyNoticeToUser1.getId();
//
//            EjyyNoticeToUser noticeToUser = ejyyNoticeToUserMapper.selectById(ToUserId);
//            NoticeCreateQuery noticeCreateQuery = new NoticeCreateQuery();
//            //拷贝数据
//            BeanUtil.copyProperties(noticeToUser,noticeCreateQuery);
//            //查询刚添加到数据库数据
//            EjyyNoticeTpl ejyyNoticeTpl1 = ejyyNoticeTplMapper.selectById(noticeTplId);
//            //拷贝数据
//            BeanUtil.copyProperties(ejyyNoticeTpl1,noticeCreateQuery);
//
//            if (!noticeCreateQuery.getPublished() || !ObjectUtil.isEmpty(noticeCreateQuery.getTpl()) ){
//                return noticeCreateQuery;
//            }
//
//            EjyyBuildingInfo ejyyBuildingInfo = ejyyBuildingInfoMapper.selectById(noticeToUser.getCommunityId());
//            Long communityId = ejyyBuildingInfo.getCommunityId();
        }

    }

    /**
     * 查询用户通知详细信息
     * @return
     */
    @Override
    public DetailNoticeVo PagingCondition(InfoQuery infoQuery) {
        Long id = infoQuery.getId();
        Long communityId = infoQuery.getCommunityId();
        //创建条件构造器
        LambdaQueryWrapper<EjyyNoticeToUser> wrapper = new LambdaQueryWrapper<>();
        //条件查询
        wrapper.eq(EjyyNoticeToUser::getId,id);
        wrapper.eq(EjyyNoticeToUser::getCommunityId,communityId);
        //查询用户通知
        EjyyNoticeToUser ejyyNoticeToUser = ejyyNoticeToUserMapper.selectOne(wrapper);
        //判断用户通知是否有数据
        if (ObjectUtil.isEmpty(ejyyNoticeToUser)){
            throw new BmException(CodeEnum.DATA_ACCESS_EXCEPTION);
        }

        EjyyNoticeTpl ejyyNoticeTpl = null;
        //判断NoticeTplId是否为空
        if(!ObjectUtil.isEmpty(ejyyNoticeToUser.getNoticeTplId())){
            //查询通知模板
            ejyyNoticeTpl = ejyyNoticeTplMapper.selectById(ejyyNoticeToUser.getNoticeTplId());
        }
        //查询房地产公司用户
        EjyyPropertyCompanyUser companyUser = propertyCompanyUserMapper.selectById(communityId);
        //判断房地产公司用户数据是否为空
        if(ObjectUtil.isEmpty(companyUser)){
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }

        //创建vo对象封装数据
        DetailNoticeVo detailNoticeVo = new DetailNoticeVo();
        //拷贝对象
        BeanUtil.copyProperties(ejyyNoticeToUser,detailNoticeVo);
        //判断通知模板数据是否为空
        if(!ObjectUtil.isEmpty(ejyyNoticeTpl)){
            //将查询到通知模板放入detailNoticeVo
            detailNoticeVo.setTpl(ejyyNoticeTpl.getTpl());
            detailNoticeVo.setTplContent(ejyyNoticeTpl.getContent());
        }
        detailNoticeVo.setRealName(companyUser.getRealName());
        //判断查询的数据是否为空
//        if (ObjectUtil.isEmpty(detailNoticeVo)){
//            throw new BmException(CodeEnum.DATA_ACCESS_EXCEPTION);
//        }
        //判断用户通知发布者是否为空
        if (!ObjectUtil.isEmpty(detailNoticeVo.getPublishedBy())){
            //查询房地产公司用户
            EjyyPropertyCompanyUser ejyyPropertyCompanyUser = propertyCompanyUserMapper.selectById(detailNoticeVo.getPublishedBy());
            //获取房地产公司用户id
            Integer userId = ejyyPropertyCompanyUser.getId();
            detailNoticeVo.setPropertyCompanyUserId(userId);
        }
        return detailNoticeVo;
    }

    /**
     * 修改用户通知信息
     * @param updateNoticeVo
     * @return
     */
    @Override
    @Transactional
    public Boolean updateNotice(UpdateNoticeVo updateNoticeVo) {
        //判断id是否为空
        if (!ObjectUtil.isEmpty(updateNoticeVo.getId())){
            //创建条件构造器
            LambdaQueryWrapper<EjyyNoticeToUser> wrapper = new LambdaQueryWrapper<>();
            //条件查询
            wrapper.eq(EjyyNoticeToUser::getId,updateNoticeVo.getId());
            wrapper.eq(EjyyNoticeToUser::getCommunityId,updateNoticeVo.getCommunityId());
            //查询用户通知
            EjyyNoticeToUser ejyyNoticeToUserById = ejyyNoticeToUserMapper.selectOne(wrapper);

            //判断用户通知id是否为有效数据和已发布的通知不能进行修改
            if (ObjectUtil.isEmpty(ejyyNoticeToUserById) || ejyyNoticeToUserById.getPublished()){
                return false;
            }

            //判断NoticeTplId是否为空
            if (!ObjectUtil.isEmpty(ejyyNoticeToUserById.getNoticeTplId())){
                EjyyNoticeTpl ejyyNoticeTpl=new EjyyNoticeTpl();
                //拷贝数据
                BeanUtil.copyProperties(updateNoticeVo,ejyyNoticeTpl);
                //修改通知模板数据
                ejyyNoticeTplMapper.updateById(ejyyNoticeTpl);

                EjyyNoticeToUser ejyyNoticeToUser = new EjyyNoticeToUser();
                //拷贝数据
                BeanUtil.copyProperties(updateNoticeVo,ejyyNoticeToUser);
                //修改用户通知
                int update = ejyyNoticeToUserMapper.updateById(ejyyNoticeToUser);
                //判断是否修改成功
                if (update<=0){
                    return false;
                }
            }
        }else {
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        return true;
    }

    /**
     * 用户已发部通知
     */
    @Override
    public Boolean Published(InfoQuery infoQuery) {
        Long id = infoQuery.getId();
        Long communityId = infoQuery.getCommunityId();
        //判读id是否为空
        if (ObjectUtil.isEmpty(id)){
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        //查询用户通知
        LambdaQueryWrapper<EjyyNoticeToUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EjyyNoticeToUser::getId,id);
        lqw.eq(EjyyNoticeToUser::getCommunityId,communityId);
        EjyyNoticeToUser detail = ejyyNoticeToUserMapper.selectOne(lqw);

        //判断是否查询到数据
        if(ObjectUtil.isEmpty(detail)){
            return false;
        }
        //判断通知是否发布
        if(detail.getPublished()){
            return true;
        }
        //创建条件构造器
        LambdaQueryWrapper<EjyyNoticeToUser> noticeLqw = new LambdaQueryWrapper<>();
        //条件查询
        noticeLqw.eq(EjyyNoticeToUser::getId,id);
        noticeLqw.eq(EjyyNoticeToUser::getCommunityId,communityId);
        detail.setPublished(true);
        detail.setPublishedBy(Constants.ADMIN_ID);
        //时间格式化
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = dateFormat.format(date);
        detail.setPublishedAt(Long.parseLong(format));
        //调用mapper
        int flagCount = ejyyNoticeToUserMapper.update(detail, lqw);
        if(flagCount <= 0){
            return false;
        }
        // 微信查询 公众号发表通知

        return true;
    }
}
